# -*- coding:utf-8 -*-
# @Author   : ZGS
# @Time     : 2021/1/6 15:15
# @FileName : tx_spider.py
# 我拿青春赌明天!
import requests
import json
from tools.pymysql_ import PyMysql
"""
腾讯的疫情网页
https://news.qq.com/zt2020/page/feiyan.htm?from=timeline&isappinstalled=0#/
建表语句:
CREATE TABLE `area_show` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `update_time` datetime DEFAULT NULL COMMENT '数据最后更新时间',
  `area_name` varchar(50) DEFAULT NULL COMMENT '地区名',
  `confirm` int(11) DEFAULT NULL COMMENT '累计确诊',
  `now_confirm` int(11) DEFAULT NULL COMMENT '现有确诊',
  `heal` int(11) DEFAULT NULL COMMENT '累计治愈',
  `dead` int(11) DEFAULT NULL COMMENT '累计死亡',
  `dead_rate` float DEFAULT NULL COMMENT '死亡率',
  `heal_rate` float DEFAULT NULL COMMENT '治愈率',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=103 DEFAULT CHARSET=utf8mb4;

CREATE TABLE `today_show` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `confirm` int(11) DEFAULT NULL COMMENT '累计确诊',
  `confirm_add` int(11) DEFAULT NULL COMMENT '新增确诊',
  `heal` int(11) DEFAULT NULL COMMENT '累计治愈',
  `heal_add` int(11) DEFAULT NULL COMMENT '当日新增治愈',
  `dead` int(11) DEFAULT NULL COMMENT '累计死亡',
  `dead_add` int(11) DEFAULT NULL COMMENT '当日新增死亡',
  `new_confirm` int(11) DEFAULT NULL COMMENT '现有确诊',
  `new_confirm_add` int(11) DEFAULT NULL COMMENT '当日新增确诊',
  `input` int(11) DEFAULT NULL COMMENT '境外输入',
  `input_add` int(11) DEFAULT NULL COMMENT '新增境外输入',
  `no_symptom` int(11) DEFAULT NULL COMMENT '无症状感染者',
  `no_symptom_add` int(11) DEFAULT NULL COMMENT '新增无症状感染者',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;
"""


def get_tx_data():
    """
        腾讯数据大屏接口
    :return:
    """
    url = "https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5"

    res = requests.get(url)
    res = json.loads(res.text)
    dict_info = json.loads(res['data'])

    print(dict_info['lastUpdateTime'])
    china_total = dict_info['chinaTotal']
    print(china_total)
    # 全国综合统计
    confirm = china_total['confirm']    # 累积确诊
    heal = china_total['heal']      # 累积治愈
    dead = china_total['dead']      # 累积死亡
    now_confirm = china_total['nowConfirm']     # 现有确诊
    imported_case = china_total['importedCase']     # 境外输入
    no_infect = china_total['noInfect']     # 无症状感染者
    print("-----------------------------------------------------")
    china_add = dict_info['chinaAdd']
    print(china_add)
    # 数据变动, 新增
    confirm_add = china_add['confirm']    # 新增累积确诊
    heal_add = china_add['heal']      # 新增累积治愈
    dead_add = china_add['dead']      # 新增累积死亡
    now_confirm_add = china_add['nowConfirm']     # 新增现有确诊
    imported_case_add = china_add['importedCase']     # 新增境外输入
    no_infect_add = china_add['noInfect']     # 新增无症状感染者
    # 打包写入数据库中
    dict_ = {
        "confirm": confirm,
        "confirm_add": confirm_add,
        "heal": heal,
        "heal_add": heal_add,
        "dead": dead,
        "dead_add": dead_add,
        "new_confirm": now_confirm,
        "new_confirm_add": now_confirm_add,
        "input": imported_case,
        "input_add": imported_case_add,
        "no_symptom": no_infect,
        "no_symptom_add": no_infect_add,
    }
    mysqlw = PyMysql()
    mysqlw.insert_table(table='today_show', dic=dict_)
    # 地区树
    area_tree = dict_info['areaTree']
    print(area_tree, '--------------')
    total = area_tree[0]['total']
    #
    print(total)

    children = area_tree[0]['children']
    print(children)
    # 下边可以拿到每个地区的数据, 最新更新数据
    for chil in children:
        print(chil.keys())
        print(chil['name'])
        print(chil['today'])
        print(chil['total'])
        area_name = chil['name']
        nowConfirm = chil['total']['nowConfirm']    # 现有确诊
        confirm = chil['total']['confirm']          # 累计确诊
        dead = chil['total']['nowConfirm']          # 累计死亡
        deadRate = chil['total']['deadRate']        # 死亡率
        heal = chil['total']['heal']        # 累计治愈
        healRate = chil['total']['healRate']        # 治愈率
        # 建表写入数据库
        dic_ = {'update_time': dict_info['lastUpdateTime'],
                'area_name': area_name,
                'confirm': confirm,
                'now_confirm': nowConfirm,
                'heal': heal,
                'dead': dead,
                'dead_rate': deadRate,
                'heal_rate': healRate,
                }
        mysqlw = PyMysql()
        mysqlw.insert_table(table='area_show', dic=dic_)


if __name__ == '__main__':
    get_tx_data()
